package cn.com.io.day20151214.control;

import cn.com.io.day20151214.pojo.Orientation;

import java.io.*;

/**
 * 自定义序列化（四）
 */
public class OrientationTest {
    public static void main(String[] args) throws IOException {
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;

        try {
            oos = new ObjectOutputStream(new FileOutputStream("orientation.txt"));
            oos.writeObject(Orientation.HORIZONTAL);

            ois = new ObjectInputStream(new FileInputStream("orientation.txt"));
            Orientation ori = (Orientation)ois.readObject();
            System.out.println(Orientation.HORIZONTAL == ori);//若Orientation类中不添加readResolve,会返回false，原因是反序列化机制在恢复Java对象进无须调用构造器来初始化Java对象。
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (oos !=null){
                oos.close();
            }
            if(ois !=null){
                ois.close();
            }
        }

    }
}
